<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Smart Retry任务管理平台</title>
    <link href="/job/css/bootstrap.min.css" rel="stylesheet">
    <style type="text/css">
	    .table {table-layout:fixed;}
	    .job-status {
	        text-align:center;
	        margin:0 auto;
	    }
    </style>
</head>
<body>
<div class="container-fluid">
    <nav class="navbar navbar-default" role="navigation">
        <div class="navbar-header">
            <a class="navbar-brand" href="#">Smart Retry任务管理平台</a>
        </div>
    </nav>
</div>
<div class="container">
    <div class="panel panel-default">
        <table class="table table-bordered" id="jobtable">
            <tr>
                <td style="width: 20%;text-align:center">identity</td>
                <td style="width: 32%;text-align:center;">名称</td>
                <td style="width: 10%;text-align:center;">执行周期</td>
                <td style="width: 15%;text-align:center;">下次重试时间</td>
                <td style="width: 8%;text-align:center;">状态</td>
                <td style="width: 15%;text-align:center;">操作</td>
            </tr>
        </table>
    </div>
</div>
<script src="/job/js/jquery.min.js"></script>
<script type="text/javascript">
    function stopJob(identity) {
        if(confirm('你确定要停止该任务吗')) {
            $.ajax({
                type: 'POST',
                url: "/job/stop?identity="+identity,
                success: function(data) {
                    window.location.reload();
                }
            });
        }
    }

    function runJob(identity) {
        if(confirm('你确定要执行该任务吗')) {
            $.ajax({
                type: 'POST',
                url: "/job/run?identity="+identity,
                success: function(data) {
                    window.location.reload();
                }
            });
        }
    }

    function startJob(identity) {
        $.ajax({
            type: 'POST',
            url: "/job/start?identity="+identity,
            success: function(data) {
                window.location.reload();
            }
        });
    }
    $(document).ready(function() {
        $.ajax({
            type: 'GET',
            url: "/job/getList",
            dataType: "json",
            success: function(data) {
                var list = data;
                var html = "";
                for(var i = 0; i < list.length; i++) {
                    var row = list[i];
                    html += "<tr>";
                    html += "<td>"+row['identity']+"</td>";
                    html += "<td>"+row['name']+"</td>";
                    html += "<td>"+row['period']+"</td>";
                    html += "<td align='center'>"+row['nextTime']+"</td>";
                    html += "<td align='center' valign='middle'>"+row['status']+"</td>";
                    html += "<td align='center'>";
                    if(row['status'] == '运行中') {
                        html += "<button class='btn btn-warning' type='button' onClick='stopJob(\""+row['identity']+"\")'>停止</button>";
                        html += "&nbsp;&nbsp;<button class='btn btn-primary' type='button' onClick='runJob(\""+row['identity']+"\")'>执行</button>";
                    } else {
                        html += "<button class='btn btn-primary' type='button' onClick='startJob(\""+row['identity']+"\")'>启动</button>";
                    }
                    html += "</td>";
                }
                $("#jobtable tr").after(html);
            }
        });
    });
</script>
</body>
</html>